/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.balloon.servlet;

import com.balloon.io.CityDao;
import com.balloon.logic.City;
import com.balloon.logic.Property;
import com.balloon.logic.Sightspot;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * This class is used for controlling front and back communication.
 */
@WebServlet(name = "FlorenceServlet", urlPatterns = {"/Florence.do"})
public class FlorenceServlet extends HttpServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        String cityName = request.getParameter("city");
        City city = new City(cityName);
        City temp = CityDao.getCityInformation(city);
        StringBuffer sb = new StringBuffer();
        response.setContentType("text/xml");
        PrintWriter out = response.getWriter();
        sb.append("<?xml version='1.0' encoding='UTF-8' ?>");
        sb.append("<city><citylongitude>" + temp.getLocationX() + "</citylongitude><citylatitude>"
                + temp.getLocationY() + "</citylatitude>");

        ArrayList<Sightspot> tempsightspot=city.getSightspots();
        for(int i=0;i<tempsightspot.size();i++){
            sb.append("<sightSpot>");
            sb.append("<sightname>");
            sb.append(tempsightspot.get(i).getName());
            sb.append("</sightname>");
            sb.append("<sightlongitude>");
            sb.append(tempsightspot.get(i).getLocationX()+"");
            sb.append("</sightlongitude>");
            sb.append("<sightlatitude>");
            sb.append(tempsightspot.get(i).getLocationY()+"");
            sb.append("</sightlatitude>");
            sb.append("<sightdescription>");
            sb.append(tempsightspot.get(i).getDescription());
            sb.append("</sightdescription>");
            sb.append("</sightSpot>");
        }

        ArrayList<Property> tempproperty=city.getProperties();
        for(int i=0;i<tempproperty.size();i++){
            sb.append("<property>");
            sb.append("<propertyname>");
            sb.append(tempproperty.get(i).getName());
            sb.append("</propertyname>");
            sb.append("<propertylongitude>");
            sb.append(tempproperty.get(i).getLocationX()+"");
            sb.append("</propertylongitude>");
            sb.append("<propertylatitude>");
            sb.append(tempproperty.get(i).getLocationY()+"");
            sb.append("</propertylatitude>");
            sb.append("<propertydescription>");
            sb.append(tempproperty.get(i).getDescription());
            sb.append("</propertydescription>");
            sb.append("</property>");
        }

        sb.append("</city>");
        //sb.append("<city><cityname>Rome</cityname><citylongitude>41.8954656</citylongitude><citylatitude>12.4823243</citylatitude><citydescription>Rome is 1111.asdfsadfs</citydescription><citylink>http://www.g.cn</citylink></city><city><cityname>Florence</cityname><citylongitude>43.7687324</citylongitude><citylatitude>11.2569013</citylatitude><citydescription>Florence is ...asdfsadfs</citydescription><citylink>http://www.g.cn</citylink></city></country>");

        out.print(sb.toString());
        //System.out.println(sb.toString());

    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
